if (typeof(UI) === "undefined") {
    var UI = {};
}

// Class: UI.Track

// Constructor: constructor
//
// Parameters:
//     scorer - TTRScorer instance to register track with.
//     from - UI.City instance of origin city for the track.
//     to - UI.City instance of destination city for the track.
//     data - Track data.
//     layer - KineticJS layer to draw track on.
UI.Track = function (scorer, from, to, data, layer) {
    this.events = {
        // Event: clicked
        // Fired when this track is clicked.
        "clicked": new TypedEvent(),

        // Event: tapped
        // Fired when this track is tapped.
        "tapped": new TypedEvent(),

        // Event: entered
        // Fired when this track is entered.
        "entered": new TypedEvent(),

        // Event: exited
        // Fired when this track is exited.
        "exited": new TypedEvent()
    };

    this.from = from;
    this.to = to;
    this.layer = layer;

    this.track = new Track(this.from.getCity(), this.to.getCity(), data.segments.length);
    scorer.getBoard().addTrack(this.track);

    this.segments = [];
    data.segments.forEach(function (data) {
        var segment = new UI.Segment(data, layer);
        this.segments.push(segment);

        segment.events.clicked.on($.proxy(function () {
            this.events.clicked.fire();
        }, this));
        segment.events.tapped.on($.proxy(function () {
            this.events.tapped.fire();
        }, this));
        segment.events.entered.on($.proxy(function () {
            this.events.entered.fire();
        }, this));
        segment.events.exited.on($.proxy(function () {
            this.events.exited.fire();
        }, this));
    }, this);

    this.layer.draw();
};

// Method: getTrack
// Retrieves the TTRScorer Track object for this track.
UI.Track.prototype.getTrack = function () {
    return this.track;
};

// Method: getFrom
// Retrieves the from city for this track.
UI.Track.prototype.getFrom = function () {
    return this.from;
};

// Method: getTo
// Retrieves the to city for this track.
UI.Track.prototype.getTo = function () {
    return this.to;
};

// Method: highlight
// Highlights this track with a border and optional color.
//
// Parameters:
//     color - Color to highlight track with. Optional.
//             If undefined, the track will remain its normal color.
UI.Track.prototype.highlight = function (color) {
    this.segments.forEach(function (segment) {
        segment.highlight(color);
    });
};

// Method: lowlight
// Removes any highlighting on this track.
UI.Track.prototype.lowlight = function () {
    this.segments.forEach(function (segment) {
        segment.lowlight();
    });
};

// Method: hide
// Hides this track, removing it from the display.
UI.Track.prototype.hide = function () {
    this.segments.forEach(function (segment) {
        segment.hide();
    });
};

// Method: show
// Restores this track to the display.
UI.Track.prototype.show = function () {
    this.segments.forEach(function (segment) {
        segment.show();
    });
};

// Method: reset
// Resets the display of this track.
UI.Track.prototype.reset = function () {
    this.segments.forEach(function (segment) {
        segment.reset();
    });
};
